/* 
 * File:   headerm.h
 * Author: ben
 *
 * Created on March 30, 2009, 9:07 AM
 */

#ifndef _HEADERM_H
#define	_HEADERM_H

#include "recommend.h"


using namespace std;


#define N_USERS 943
#define N_MOVIES 1682

int AnalyseAll(int mode,int argc,char** argv) {
    int i, n_tot = N_USERS, ib, counter = 0;
    class recommend test(N_USERS, N_MOVIES);
    double tot = 0, res;
    ostringstream fname;
    Matrix results = Matrix(4, 10);

    ofstream fout("results_output.dat");


    for (ib = 50; ib <= 59; ib++) {

        cout << "Bucket " << ib << endl;
        fname.str("");
        fname << "data/test_" << ib << ".dat";
        //fname.str("data/TestSetPR.txt");

        test.LoadTest_set(fname.str());
        fname.str("");
        fname << "data/training_" << ib << ".dat";
        //fname.str("data/TrainingSetPR.txt");


        test.LoadTraining_set(fname.str());

        switch (mode) {
            case 1:
                test.SuggestMeSomethingMaxF();
                break;
            case 2:
                test.SuggestMeSomethingLp();
                break;
            case 3:
                test.SuggestMeSomethingLpUserBased(atoi(argv[2]));
                break;
            case 4:
                test.SuggestMeSomethingLpMovieBased(atoi(argv[2]));
                break;
            case 5:
                test.SuggestMeSomethingWWtUserBased(atoi(argv[2]));
                break;
            case 6:
                test.SuggestMeSomethingWWtMovieBased(atoi(argv[2]));
                break;
            case 7:
                test.SuggestMeSomethingBinaryUserBased(atoi(argv[2]));
                break;
            case 8:
                test.SuggestMeSomethingDWalk(MODE_USERS, atoi(argv[2]), atoi(argv[3]));
                break;
            case 9:
                test.SuggestMeSomethingDWalk(MODE_MOVIES, atoi(argv[2]), atoi(argv[3]));
                break;
            case 10:
                test.SuggestMeSomethingDWalkUserBased(atoi(argv[2]), atoi(argv[3]));
                break;
        }

        //test.SuggestMeSomethingMaxF();
        //test.SuggestMeSomethingLp();
        //test.SuggestMeSomethingLpUserBased();
        //test.SuggestMeSomethingLpMovieBased();
        //test.SuggestMeSomethingWWtUserBased();
        //test.SuggestMeSomethingWWtMovieBased();
        //test.SuggestMeSomethingBinaryUserBased();

        tot = 0;
        n_tot = N_USERS;
        for (i = 0; i < N_USERS; i++) {
            res = test.AnalysisDegreeOfAgreement(i);
            fout << res;
            //cout << res << " ; " ;
            if (res >= 0) {
                tot += res;
            } else {
                n_tot--;
            }
        }
        cout << "Doa: " << tot / n_tot << endl;
        results(0, counter) = tot / n_tot;
        fout << endl;

        //cout << test.AnalysisPercentile(2) << endl;
        n_tot = N_USERS;
        tot = 0;
        for (i = 0; i < N_USERS; i++) {
            res = test.AnalysisPercentile(i);
            fout << res;
            //cout << res << " ; " ;
            if (res >= 0) {
                tot += res;
            } else {
                n_tot--;
            }
        }
        cout << "Percentile: " << tot / n_tot << endl;
        results(3, counter) = tot / n_tot;
        fout << endl;


        n_tot = N_USERS;
        tot = 0;
        for (i = 0; i < N_USERS; i++) {
            res = test.AnalysisRecall(i, 20);
            fout << res;
            //cout << res << " ; " ;
            if (res >= 0) {
                tot += res;
            } else {
                n_tot--;
            }
        }
        cout << "Recall 20: " << tot / n_tot << endl;
        results(1, counter) = tot / n_tot;
        fout << endl;

        n_tot = N_USERS;
        tot = 0;
        for (i = 0; i < N_USERS; i++) {
            res = test.AnalysisRecall(i, 10);
            fout << res;
            //cout << res << " ; " ;
            if (res >= 0) {
                tot += res;
            } else {
                n_tot--;
            }
        }
        cout << "Recall 10: " << tot / n_tot << endl;
        results(2, counter) = tot / n_tot;
        fout << endl;

        counter++;

    }

    fout.close();

    cout << "results" << endl;
    cout << results << endl;
    ofstream fout1("results.dat");
    //fout1 << "Doa, Recall10, Recall20, Percentile" << endl;
    fout1 << results << endl;
    fout1.close();
}

int Showhelp() {
    cout << "====================================================================" << endl;
    cout << "     RECOMMENDATION TOOLS" << endl;
    cout << "====================================================================" << endl;
    cout << "Benoit Vermoortel, Mech.E." << endl;
    cout << "Benoit.Vermoortel@gmail.com" << endl;
    cout << "Thesis at FUCaM, 2008" << endl;
    cout << "http://code.google.com/p/recommendtools/" << endl;
    cout << "====================================================================" << endl;
    cout << "maxf			- MaxF method" << endl;
    cout << "lp			- L+ method" << endl;
    cout << "lp-userbased k		- L+ user based (needs the number of neighbours k)" << endl;
    cout << "lp-moviebased k	- L+ movie based (needs the number of neighbours k)" << endl;
    cout << "wwt-userbased k	- WWt user based (needs the number of neighbours k)" << endl;
    cout << "wwt-moviebased	k	- WWt movie based (needs the number of neighbours k)" << endl;
    cout << "binary-userbased k	- binary method user based (needs the number of neighbours k)" << endl;
    cout << "dwalk-movies k		-D-walks (needs the max pathlength L)" << endl;
    cout << "dwalk-direct k L	-D-walks (needs the number of neighbours k and L)" << endl;
    cout << "dwalk-users k L	-D-walks (needs the number of neighbours k and L)" << endl;
    cout << "====================================================================" << endl;
}

int AnalysePRfiles(int mode, int argc, char** argv) {
    int i, n_tot = N_USERS, ib;
    class recommend test(N_USERS, N_MOVIES);
    double tot = 0, res;
    ostringstream fname;
    string stemp;
    Matrix results = Matrix(4, 1);
    //CHARGEMENT DES INPUT/OUTPUT
    fname.str("data/TestSetPR.txt");
    //fname.str("data/mytest/test.dat");
    test.LoadTest_set(fname.str());
    fname.str("data/TrainingSetPR.txt");
    //fname.str("data/mytest/training.dat");
    test.LoadTraining_set(fname.str());

    //test.DWalk(MODE_USERS);
    ofstream fout("results_output.dat");

    //CHOIX DE LA TECHNIQUE
    switch (mode) {
        case 1:
            test.SuggestMeSomethingMaxF();
            break;
        case 2:
            test.SuggestMeSomethingLp();
            break;
        case 3:
            test.SuggestMeSomethingLpUserBased(atoi(argv[2]));
            break;
        case 4:
            test.SuggestMeSomethingLpMovieBased(atoi(argv[2]));
            break;
        case 5:
            test.SuggestMeSomethingWWtUserBased(atoi(argv[2]));
            break;
        case 6:
            test.SuggestMeSomethingWWtMovieBased(atoi(argv[2]));
            break;
        case 7:
            test.SuggestMeSomethingBinaryUserBased(atoi(argv[2]));
            break;
        case 8:
            test.SuggestMeSomethingDWalkMovieBased(atoi(argv[2]), atoi(argv[3]));
            break;
        case 9:
            test.SuggestMeSomethingDWalk(MODE_MOVIES, atoi(argv[2]), atoi(argv[3]));
            break;
        case 10:
            test.SuggestMeSomethingDWalkUserBased(atoi(argv[2]), atoi(argv[3]));
            break;
    }
    //test.SuggestMeSomethingMaxF();
    //test.SuggestMeSomethingLp();
    //test.SuggestMeSomethingLpUserBased();
    //test.SuggestMeSomethingLpMovieBased();
    //test.SuggestMeSomethingWWtUserBased(atoi(argv[1]));
    //test.SuggestMeSomethingWWtMovieBased(atoi(argv[1]));
    //test.SuggestMeSomethingBinaryUserBased();

    // CALCUL DES INDICATEURS
    //==================================
    tot = 0;
    n_tot = N_USERS;
    for (i = 0; i < N_USERS; i++) {
        res = test.AnalysisDegreeOfAgreement(i);
        fout << res;
        //cout << res << " ; " ;
        if (res >= 0) {
            tot += res;
        } else {
            n_tot--;
        }
    }
    cout << "Doa: " << tot / n_tot << endl;
    results(0, 0) = tot / n_tot;
    fout << endl;

    //cout << test.AnalysisPercentile(2) << endl;

    n_tot = N_USERS;
    tot = 0;
    for (i = 0; i < N_USERS; i++) {
        res = test.AnalysisPercentile(i);
        fout << res;
        //cout << res << " ; " ;
        if (res >= 0) {
            tot += res;
        } else {
            n_tot--;
        }
    }
    cout << "Percentile: " << tot / n_tot << endl;
    results(3, 0) = tot / n_tot;
    fout << endl;


    n_tot = N_USERS;
    tot = 0;
    for (i = 0; i < N_USERS; i++) {
        res = test.AnalysisRecall(i, 20);
        fout << res;
        //cout << res << " ; " ;
        if (res >= 0) {
            tot += res;
        } else {
            n_tot--;
        }
    }
    cout << "Recall 20: " << tot / n_tot << endl;
    results(1, 0) = tot / n_tot;
    fout << endl;

    n_tot = N_USERS;
    tot = 0;
    for (i = 0; i < N_USERS; i++) {
        res = test.AnalysisRecall(i, 10);
        fout << res;
        //cout << res << " ; " ;
        if (res >= 0) {
            tot += res;
        } else {
            n_tot--;
        }
    }
    cout << "Recall 10: " << tot / n_tot << endl;
    results(2, 0) = tot / n_tot;
    fout << endl;






    //==================================

    fout.close();

    cout << "results" << endl;
    cout << results << endl;
    stemp = argv[1];
    stemp = "output/" + stemp + ".dat";
    ofstream fout1(stemp.data());
    fout1 << "Doa, Recall20, Recall10, Percentile" << endl;
    fout1 << results << endl;
    fout1.close();
    return (0);
}

int AnalyseWeb(int mode, int argc, char ** argv, string intest, string intraining) {
    class recommend test(68, 74);
    //CHARGEMENT DES INPUT/OUTPUT

    test.LoadTest_set_3lines_R(intest);

    test.LoadTraining_set_R(intraining);

    //test.DWalk(MODE_USERS);
    //ofstream fout("results_output.dat");

    //CHOIX DE LA TECHNIQUE
    switch (mode) {
        case 1:
            test.SuggestMeSomethingMaxF();
            break;
        case 2:
            test.SuggestMeSomethingLp();
            break;
        case 3:
            test.SuggestMeSomethingLpUserBased(atoi(argv[2]));
            break;
        case 4:
            test.SuggestMeSomethingLpMovieBased(atoi(argv[2]));
            break;
        case 5:
            test.SuggestMeSomethingWWtUserBased(atoi(argv[2]));
            break;
        case 6:
            test.SuggestMeSomethingWWtMovieBased(atoi(argv[2]));
            break;
        case 7:
            test.SuggestMeSomethingBinaryUserBased(atoi(argv[2]));
            break;
        case 8:
            test.SuggestMeSomethingDWalkMovieBased(atoi(argv[2]), atoi(argv[3]));
            break;
        case 9:
            test.SuggestMeSomethingDWalk(MODE_MOVIES, atoi(argv[2]), atoi(argv[3]));
            break;
        case 10:
            test.SuggestMeSomethingDWalkUserBased(atoi(argv[2]), atoi(argv[3]));
    }
    test.PutSuggestionsInFile("results4web.dat");
    return (0);
}
#endif	/* _HEADERM_H */

